﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


namespace RN.ERP.Web.Person
{
    public partial class PersonUploadFile_BK : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initial();
            }
        }
        /// <summary>
        /// 取得参数初始化
        /// </summary>
        private void Initial()
        {
            if (Request.Params["filename"] == null)
            {
                Manager1.RadAlert("文件名不能为空！", 300, 150, "提示", "");
                return;
            }
            if (Request.Params["filename"] != null)
            {
                hdfilename.Value = Request.Params["filename"].Trim();
            }
            if (Request.Params["SysCode"] != null)
            {
                hdSysCode.Value = Request.Params["SysCode"].Trim();
            }
            if (Request.Params["FuncCode"] != null)
            {
                hdFuncCode.Value = Request.Params["FuncCode"].Trim();
            }
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="sysCode"></param>
        /// <param name="funcCode"></param>
        private string UploadFiles(string filename, string sysCode, string funcCode)
        {
            try
            {
                string filesType = ".jpg";
                string sFileName = String.Empty;
                string[] FileNames;
                char[] flag = { '\\' };
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFile hpf = Request.Files[i];
                    FileNames = hpf.FileName.Split(flag);
                    sFileName = FileNames[FileNames.Length - 1];
                    if (sFileName == String.Empty)//判断上传的文件是否为空
                    {
                        continue;
                    }
                    string sExtension = Path.GetExtension(sFileName);
                    if (filesType.ToLower().IndexOf(sExtension.ToLower()) != -1)//判断文件类型
                    {
                        //生成文件名
                        string filenm = filename + filesType;
                      //  byte[] fileInfo = new byte[hpf.InputStream.Length];
                        Stream stream = hpf.InputStream;
                        byte[] bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                        FileService.FileWebServiceSoapClient fileServiceClient = new FileService.FileWebServiceSoapClient();
                        ////上传服务返回结果：1,上传成功！；0,错误信息
                        return fileServiceClient.UploadImageBytes(bytes, filenm, sysCode, funcCode, true, "", "");
                    }
                }
            }
            catch { return string.Empty; }
            return string.Empty;
        }
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnUpload_Click(object sender, EventArgs e)
        {
            string result = UploadFiles(hdfilename.Value, hdSysCode.Value, hdFuncCode.Value);
            if (string.IsNullOrEmpty(result))
            {
                Manager1.RadAlert("上传失败！", 300, 150, "提示", "");
                return;
            }
            string result1 = result.Split(',')[0];
            string message = result.Split(',')[1];
          // Manager1.RadAlert(message, 300, 150, "提示", "");
            if (result1 == "1")
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "closewin", "<script>returnToParent(0);</script>");
            }
            else
            {
                Manager1.RadAlert(message, 300, 150, "提示", "");
                return;
            }
        }
      
    }
}